*********************************************************************************************
* This do file contains the code used to produce in-text statistics not found in exhibits (for version submitted on 2020 April 20th)-- this do file may change depending on new edits
*********************************************************************************************

**********************************************************

set more off
clear
clear mata
program drop _all
capture log close
set logtype text
clear matrix
set maxvar 10000
set memory 500m
set trace off
eststo clear

*Additional packages to install if not already installed: 
/*
ssc install estout
ssc install dsconcat
ssc install qqvalue
ssc install texsave
*/

*set user directory; please set the macro "root" to the base directory of the directory containing the extracted content of the zip-file
glo root "C:\Users\tbrai\Dropbox\DBT\Data\ePOS RCT Jharkhand\ABBA replication package_20200612"   //
di in red "The root directory is set to ${root}"


*set directories to load data, user-defined functions/programs/ado files, and store output  
glo AdminDataDir "${root}/Data/Admin"
glo SurveyDataDir "${root}/Data/Survey"
glo OutputDir "C:\Users\tbrai\Dropbox\github_private\ABBA\replication\exhibits"
glo CodeDir "C:\Users\tbrai\Dropbox\github_private\ABBA\replication\code"
glo functionDir "${root}/AnalysisCode/Lib/Functions"
glo adoDir "${root}/AnalysisCode/Lib/Ado"


*load programs/functions files written by RAs working on this project
*qui include "${functionDir}/AttritionTable_program_svy.do"
qui include "${functionDir}/AttritionTable_program.do"
qui include "${functionDir}/AttritionTable_program_svy.do"
qui include "${functionDir}/StackingRegressionModels.do"
qui include "${functionDir}/Preferences_Perceptions_program.do"
qui include "${functionDir}/Balance_program.do"

*Change page numbers when draft is finalized, for now, include page numbers as of Draft on March 26th 


*******************
*Pg 3: 
*"Six months after treatment onset, 95% of beneficiary households in treated areas had at least one member with an Aadhaar number seeded to the PDS account
	use "${SurveyDataDir}\JH_ePOS_HH_DataforAnalysis.dta",clear

	gen notaadhaar_seeded=( inlist(b20_adr_seed_hh_1, "-88", "-89", ".", "0") & ss_code=="SS01" )    //0 means seeded, 1 means not 

	svyset [pweight=pweight]

	svy: tab notaadhaar_seeded if treatment==1

	svy: tab notaadhaar_seeded if treatment==0

	** result: 95% for treatment, 86% for control


*******************
*Pg 9
/* As of May 2016, 76.5% of ration cards in areas assigned
to treatment and 79.9% of those in areas assigned to control had been seeded with at least one
Aadhaar number. These gures had risen to 94.5% and 92.6%, respectively, by October of 2016.
Finally, by May 2018, these gures had risen further to 99.8% and 99.5%, respectively.*/


	use "${AdminDataDir}\BL_EL1_beneficiary_turnover_RC_BL_EL1.dta",clear
		
	** May 2016 **

		* gen ind for if the RC had Aadhaar seeded at BL
		gen seed_ind0=1 if uidcount0>0 & !missing(uidcount0)
		replace seed_ind0=0 if uidcount0==0

		tab seed_ind0 if treatment=="Treatment"
		tab seed_ind0 if treatment=="Control"
			** result: 76.5% for treatment, 79.9% for control


	** October 2016 **
		* gen ind for if the RC had Aadhaar seeded at BL
		gen seed_ind1=1 if uidcount1>0 & !missing(uidcount1)
		replace seed_ind1=0 if uidcount1==0

		tab seed_ind1 if treatment=="Treatment"
		tab seed_ind1 if treatment=="Control"
			** result: 94.5% for treatment, 92.6% for control

	use "${AdminDataDir}\deletions_admin.dta",clear

	** May 2018 **

		*gen ind for if the RC had Aadhaar seeded at BL
		gen seed_ind3=1 if uidcount3>0 & !missing(uidcount3)
		replace seed_ind3=0 if uidcount3==0

		tab seed_ind3 if treatment==1
		tab seed_ind3 if treatment==0
			** result: 99.8% for treatment, 99.5% for control
			
			
*******************
*P9 
*By June of 2017, ePoS devices were actively in use for authentication in 93% of FPSs in our study area, including those in control blocks

	use "${AdminDataDir}\transaction_jun17.dta",clear     //contains machine mode data for fair price shops that recorded at least one transaction in June 2017

		count
		scalar transacted=r(N)
		di transacted/9265     //9265 shops in total in 10 study district
			** result: 93% of the total universe of shops in 10 study districts had transaction data

*******************
*Pg 22
*"...base of cost of Rs. 144 per ration card per month operating the PDS"
	/*
	Cost of PDS per ration card per month = 
		Total PDS distribution cost in 2016 -2017  (obtained from State budget report): Rs.8,746,378,452
		
						divided by 
						
			Total ration cards in the state in 2016 - 2017 (obtained from beneficiary list in baseline): 5,067,861
			
			
						divided by
						
				12 months
		
		*/
		di 8746378452/5067861/12
		*result: on average, ~ Rs. 144 per ration card per month
		

*******************
*Note: this statistic was in text before but was deleted - leaving the code here in case we want to add back in

*Pg22
/*34% of FPS in treated blocks had reported opening balances worth one month or more by June of 2017 and should therefore have received no disbursement of wheat or
rice in the next month had the policy been strictly implemented. Overall, we estimate that food-grain distributions would have fallen by 43% in July had reconciliation been strictly implemented.
*/


	use "${AdminDataDir}/adherence_targetdisbursement.dta", clear

	******** % of FPSs that would have received nothing **************
	*create vars for grain
	gen ent_qty_ricewheat = ent_qty_rice  + ent_qty_wheat  //entitlement
	gen dis_ricewheat=agg_dis_rice + agg_dis_wheat         //disbursement
	gen Tgt_dis_qty_ricewheat = Tgt_qty_rice  + Tgt_qty_wheat //target disbursement calculated from NIC records


	merge m:1 admin_dealer_id using "${AdminDataDir}\NICstock_cleaned_fpslevel.dta", nogen

	egen stock_grain2=rowtotal(stock_rice stock_wheat),mi

	*Count how many dealers had enough stock to cover entitlment
	gen diff_grain=stock_grain2-ent_qty_ricewheat
	sum diff_grain if month==7,de
	count if diff_grain>=0 & !missing(diff_grain) & month==7

	di 3157/9265
		**result: 34% of dealers would have got no disbursement


	******** % reduction in disbursement **************

	egen sum_ent_jul=sum(ent_qty_ricewheat) if month==7   //57481860
	egen sum_target_jul=sum(Tgt_dis_qty_ricewheat ) if month==7   //32597824

	gen ratio = (sum_ent_jul - sum_target_jul)/sum_ent_jul
	tab ratio

		** result: total disbursement would have fallen by 43%

*******************
*Pg 26
/*On average, the government held treated shops responsible for 7,715 kg
of grain as opposed to 3,346 kg for control shops (p < 0:0001) */


	ttest stock_grain2, by(treatment)
		** result: on average treated shops are recorded as having 7,715 kg of grain and control shops 3,346 kg (p < 0:0001)

*******************
*Pg 26
/* 40This Figure includes the 7 non-study districts which also received treatment: 13% * 12.32 million beneficiaries = 1,602,000 excluded. */

* Total individuals in 10 study and 7 non-study districts: 12321593

* Total individuals in 10 study districts, treated: 6250199
* Total individuals in 10 study districts, control: 2776573

* Total individuals in 7 non-study districts: 6071394

* Assuming non-study districts are treated
di (6250199 + 6071394) * 0.13
di 2776573 * 0.043

di 6250199 * 0.13
di (2776573 + 6071394) * 0.043

* Assuming non-study districts are control

*******************
*P27
/*In each case these gures are signicantly larger than the corresponding gures for control areas. The
proportion of the drop in value disbursed that represents exclusion error is higher in treated than in
control areas, by 34% to 22%, (though statistically we do not reject the null that these proportions
are the same, p = 0.18)*/

	*******************************************************************************
	program mydisp, eclass
			 matrix betas=e(b)
			 matrix colnames betas = "reconciliation" "recXtmint" "index"  "post_rec"
			 mat list betas
			 ereturn repost b = betas, rename
	 end
	********************************************************************************

	use "${AdminDataDir}/allocation_blocklevel_jan17_nov17.dta",clear

	*Merge HH survey data
	preserve
	use "${SurveyDataDir}/JH_ePOS_HH_reconciliation_DataforAnalysis.dta",clear

		*include only if surveyed or ghosts
		keep if surveyed==1|ghost_final==1

		*Keep only variables needed in analysis
		keep district_code block_code fps_uid uid quantity* value* pweight isurban 

	tempfile HHdata
	save `HHdata'
	restore

	merge 1:m district_code block_code isurban using `HHdata',update 


	local ration "total rice wheat sugar salt kero"
	local month_list "nov17 oct17 sep17 aug17 jul17 jun17 may17 apr17 mar17 feb17 jan17"

	*rename value vars 
	foreach mon of local month_list{
		
		rename value_totalrec_`mon' value_totalr_`mon'
		rename value_totalnonrec_`mon' value_totalnr_`mon'

	}

	scalar obs_all =  3960*11
	scalar obs_all_1 =   2610*11
	scalar obs_all_0 =  1350*11


	****total values
	*ocal rationt "totalr totalnr"
	local month_list "nov17 oct17 sep17 aug17 jul17 jun17 may17 apr17 mar17 feb17 jan17"

	local rat "totalr"
		
		local month_list "nov17 oct17 sep17 aug17 jul17 jun17 may17 apr17 mar17 feb17 jan17"
		svyset fps_uid [pw=pweight]

		keep dis_value_perRC_`rat'_*17 value*17 strata block_code index_* reconciliation_* post_reconciliation_* uid fps_uid pweight treatment

		local m = 11

		foreach mon of local month_list{
			rename dis_value_perRC_`rat'_`mon' dis_value_perRC_`rat'`m'
			rename value_`rat'_`mon' value_`rat'`m'
			rename index_`mon' index`m' 
			rename reconciliation_`mon' reconciliation`m'
			rename post_reconciliation_`mon' post_reconciliation`m'

			local m = `m' - 1
		}
		reshape long value_`rat' dis_value_perRC_`rat' index reconciliation post_reconciliation, i(uid) j(month)
		
		gen recXtmint = reconciliation*(index - 7)
		
	
	*Effect of reconciliation on leakage by Treatment/Control
	
	local rat "totalr"

	forval i=0/1{

		*Disbursement per RC
		
			*Jan Mean
			qui: mean dis_value_perRC_`rat' if month == 1 & treatment==`i'
			qui estat sd
			matrix Mean0d_`i'`rat' = r(mean)
		
			*Main estimation
			eststo `rat'd_`i': svy:reg dis_value_perRC_`rat' index reconciliation recXtmint post_reconciliation if treatment==`i'
			estadd scalar january_mean = `=Mean0d_`i'`rat'[1,1]'
			estadd scalar percent_obs 100*e(N)/obs_all
			
		*Value received reported by HHs
		
			*Jan Mean
			qui: mean value_`rat' if month == 1 & treatment==`i'
			qui estat sd
			matrix Mean0r_`i'`rat' = r(mean)
			
			*Main estimation
			eststo `rat'r_`i': svy:reg value_`rat' index reconciliation recXtmint post_reconciliation if treatment==`i'
			estadd scalar january_mean = `=Mean0r_`i'`rat'[1,1]'
			estadd scalar percent_obs 100*e(N)/obs_all
		
		}

		*Jointly estimate leakage
		eststo temp: suest totalrd_1  totalrd_0 totalrr_1  totalrr_0
		
		*difference in two ratios (delta method)
		nlcom [totalrr_1]_b[reconciliation]/[totalrd_1]_b[reconciliation]-[totalrr_0]_b[reconciliation]/[totalrd_0]_b[reconciliation], level(90)
		
		testnl [totalrr_1]_b[reconciliation]/[totalrd_1]_b[reconciliation]=[totalrr_0]_b[reconciliation]/[totalrd_0]_b[reconciliation]

		*ratio for T/C (delta method)
		nlcom [totalrr_1]_b[reconciliation]/[totalrd_1]_b[reconciliation]
		nlcom [totalrr_0]_b[reconciliation]/[totalrd_0]_b[reconciliation]
		
			** result: p=0.18

